package com.object.comm;

import java.io.IOException;
import java.net.InetSocketAddress;

import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

import com.object.handler.ServerHandler;

public class MyServer {
	public static void main(String[] args) throws IOException {
		int bindPort = 9876;
		
		IoAcceptor acceptor = new NioSocketAcceptor();
		
		acceptor.getSessionConfig().setReadBufferSize(2048);
		acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
		
		acceptor.getFilterChain().addLast("logger", new LoggingFilter());
		//设定服务器解析消息的规则是以Object对象为单位进行传输，注意此时该对象需实现Serializable接口
		acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ObjectSerializationCodecFactory()));
		
		acceptor.setHandler(new ServerHandler());
		
		acceptor.bind(new InetSocketAddress(bindPort));
		
		System.out.println("MinaServer is startup, and it's listing on := " + bindPort);
	}
}